<?php
/**
 * Created by PhpStorm.
 * User: jucai002
 * Date: 2019/1/24
 * Time: 10:25
 */

namespace app\modules\admin\controllers;


use app\models\Product;

class CommodityController extends DefaultController
{
    /**
     * @return string
     * 列表
     */
    public function actionIndex()
    {
        $like_name = \Yii::$app->request->get('keyword',false);
        $like_status = \Yii::$app->request->get('status','');
        $andwhere = '';
        if(!empty($like_name)) $andwhere .=" name like '%{$like_name}%' ";
        if($like_status>=0 && $like_status !=''){
            if(empty($andwhere)){
                $andwhere .= 'status = '.$like_status;
            }else{
                $andwhere.= " and status = {$like_status} ";
            }
        }
        if(empty($andwhere))$andwhere = 1;
        $product = Product::find()
                ->where($andwhere)
            ->orderBy('status desc ,sort ASC ')->asArray()->all();
        return $this->render('index',['head'=>$this->Index(),'end'=>$this->End(),'data'=>$product]);
    }

    /**
     * 添加
     */
    public function actionAdd()
    {
        if(empty(\Yii::$app->request->post())){
            return $this->render('add',['head'=>$this->Index(),'end'=>$this->End()]);
        }else{
            $files = $_FILES;
            $pic = $this->Up_File($files);
            $name = \Yii::$app->request->post('title',false);//名称
            $sort = \Yii::$app->request->post('sort',0);////排序
            $play_money = \Yii::$app->request->post('price',1);//玩游戏金额
            $original_price = \Yii::$app->request->post('costprice',0);//原价
            $limit = \Yii::$app->request->post('succprice',0);//成功限制次数
            $status = \Yii::$app->request->post('status',0);//是否显示
            if(empty($name) || empty($pic)) return $this->render('add',['head'=>$this->Index(),'end'=>$this->End()]);
            //入库
            $product = new Product();
            $product->name = $name;
            $product->pic = $pic;
            $product->play_money = $play_money;
            $product->original_price = $original_price;
            $product->limit = $limit;
            $product->status = $status;
            $product->sort = $sort;
            $product->time = time();
            $product->save();
            $this->redirect(\Yii::$app->urlManager->createUrl('admin/commodity/index'));
        }
    }

    public function actionOffline()
    {
        $id = \Yii::$app->request->get('id',0);
        $pro = Product::findOne($id);
        if(empty($pro)) $this->redirect(\Yii::$app->urlManager->createUrl('admin/commodity/index'));
        if($pro->status == 1){
            $pro->status = 0;
        }else{
            $pro->status = 1;
        }
        $pro->save(false);
        $this->redirect(\Yii::$app->urlManager->createUrl('admin/commodity/index'));
    }

    public function actionSave()
    {
        if(!\Yii::$app->request->post()){
            $id = \Yii::$app->request->get('id',0);
            $pro = Product::findOne($id);
            return $this->render('save',['head'=>$this->Index(),'end'=>$this->End(),'data'=>$pro]);
        }else{
            $files = $_FILES;
            $pic = $this->Up_File($files);
            $name = \Yii::$app->request->post('title',false);//名称
            $sort = \Yii::$app->request->post('sort',0);////排序
            $play_money = \Yii::$app->request->post('price',1);//玩游戏金额
            $original_price = \Yii::$app->request->post('costprice',0);//原价
            $limit = \Yii::$app->request->post('succprice',0);//成功限制次数
            $status = \Yii::$app->request->post('status',0);//是否显示
            $id = \Yii::$app->request->post('id',0);//是否显示
            if(empty($name) || empty($id)) return $this->render('add',['head'=>$this->Index(),'end'=>$this->End()]);
            $pro = Product::findOne($id);
            $pro->name = $name;
            if(!empty($pic)){
                $pro->pic = $pic;
            }
            $pro->play_money = $play_money;
            $pro->original_price = $original_price;
            $pro->limit = $limit;
            $pro->status = $status;
            $pro->sort = $sort;
            $pro->time = time();
            $pro->save();
            $this->redirect(\Yii::$app->urlManager->createUrl('admin/commodity/index'));
        }
    }
}